package cn.tedu.mvcemp.controller;

import cn.tedu.mvcemp.mapper.EmpMapper;
import cn.tedu.mvcemp.pojo.entity.Emp;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class EmpController {
    @Autowired
    private EmpMapper empMapper;

    @RequestMapping("/emp/insert")
    @ResponseBody
    public String insert(Emp emp) {
        empMapper.insert(emp);
        return "添加成功!<a href='/emp/select'>返回列表页</a>";
    }

    @RequestMapping("/emp/select")
    @ResponseBody
    public String select() {
        List<Emp> list = empMapper.select();
        String html = "<table border='1'>";
        html += "<caption>员工列表</caption>";
        html += "<tr><th>id</th><th>姓名</th><th>工资</th><th>岗位</th><th>操作</th></tr>";
        for (Emp emp : list) {
            html += "<tr>";
            html += "<td>" + emp.getId() + "</td>";
            html += "<td>" + emp.getTitle() + "</td>";
            html += "<td>" + emp.getSalary() + "</td>";
            html += "<td>" + emp.getJob() + "</td>";
            html += "<td><a href='/emp/delete?id=" + emp.getId() + "'>删除</a>" + "</td>";
            html += "</tr>";
        }
        html += "</table>";
        return html;
    }

    @RequestMapping("/emp/update")
    @ResponseBody
    public String update(Emp emp) {
        empMapper.update(emp);
        return "修改完成!<a href='/emp/select'>返回列表首页</a>";
    }

    @RequestMapping("/emp/delete")
    @ResponseBody
    public String delete(){
        empMapper.delete();
        return "删除完成!<a href='/emp/select'>返回列表页</a>";
    }
}
